﻿#if CORE6
using System;
using System.IO;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;

namespace Dapper.Sharding
{
    public class ConfigSystemTextJson
    {
        private readonly static JsonSerializerOptions opt = new JsonSerializerOptions()
        {
            //Encoder = JavaScriptEncoder.Create(UnicodeRanges.All), //不编码中文
            //PropertyNamingPolicy = null, //序列化属性不转小写
            Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,//不编码中文和html
            NumberHandling = JsonNumberHandling.AllowReadingFromString, //反序列化时允许string转数字
            PropertyNameCaseInsensitive = true, //反序列化忽略大小写
            ReadCommentHandling = JsonCommentHandling.Skip,
            AllowTrailingCommas = true
        };

        public static JsonObject LoadObject(string jsonFile)
        {
            var file = $"{AppDomain.CurrentDomain.BaseDirectory}{jsonFile}";
            var text = File.ReadAllText(file);

            return JsonSerializer.Deserialize<JsonObject>(text, opt);
        }

        public static ClientConfig LoadConfig(string jsonFile, string key = null)
        {
            var jobject = LoadObject(jsonFile);
            if (!string.IsNullOrEmpty(key))
            {
                jobject = jobject[key].AsObject();
            }

            var cfg = new ClientConfig();
            cfg.DbType = DataBaseType.MySql;
            cfg.Config = jobject.Deserialize<DataBaseConfig>(opt);

            if (jobject.TryGetPropertyValue("AutoCreateDatabase", out JsonNode node))
            {
                cfg.AutoCreateDatabase = node.GetValue<bool>();
            }

            if (jobject.TryGetPropertyValue("AutoCreateTable", out node))
            {
                cfg.AutoCreateTable = node.GetValue<bool>();
            }

            if (jobject.TryGetPropertyValue("AutoCompareTableColumn", out node))
            {
                cfg.AutoCompareTableColumn = node.GetValue<bool>();
            }

            if (jobject.TryGetPropertyValue("AutoCompareTableColumnLength", out node))
            {
                cfg.AutoCompareTableColumnLength = node.GetValue<bool>();
            }

            if (jobject.TryGetPropertyValue("AutoCompareTableColumnDelete", out node))
            {
                cfg.AutoCompareTableColumnDelete = node.GetValue<bool>();
            }

            if (jobject.TryGetPropertyValue("Database", out node))
            {
                cfg.Database = node.GetValue<string>();
            }

            if (jobject.TryGetPropertyValue("DbType", out node))
            {
                var dbType = node.GetValue<string>().ToLower();
                if (dbType == "mysql")
                {
                    cfg.DbType = DataBaseType.MySql;
                }
                else if (dbType == "postgresql" || dbType == "pgsql")
                {
                    cfg.DbType = DataBaseType.Postgresql;
                }
                else if (dbType == "sqlserver")
                {
                    cfg.DbType = DataBaseType.SqlServer;
                }
                else if (dbType == "oracle")
                {
                    cfg.DbType = DataBaseType.Oracle;
                }
                else if (dbType == "sqlite")
                {
                    cfg.DbType = DataBaseType.Sqlite;
                }
                else if (dbType == "clickhouse")
                {
                    cfg.DbType = DataBaseType.ClickHouse;
                }
            }
            return cfg;
        }
    }
}
#endif